<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="companyFormRules" :model="companyForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="companyCode">
        <el-input v-model="companyForm.companyCode" />
      </el-form-item>
      <el-form-item label="图标">
        <Upload v-model="companyForm.avatar" />
      </el-form-item>
      <el-form-item label="标题" prop="companyName">
        <el-input v-model="companyForm.companyName" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="companyForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="companyForm.status.value === 5" type="danger" @click="handleDelete(companyForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveCompany, removeCompanyById, getCompanyDetail } from '@/api/logistics/company'
import waves from '@/directive/waves' // waves directive
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      pickerOptions: {
          disabledDate(time) {
            //return time.getTime() < Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      exchangeOptions:[],
      companyForm: {
        code: '',
        title: '',
        remark: '',
        keyword:[],
        status: {
          label: '',
          value: 1
        }
      },
      companyFormRules: [],
      keywordFormHidden:true,
      keywordForm:{
        title:""
      }
    }
  },
  created() {
    let companyID = this.$route.query && this.$route.query.companyID
    if (companyID !== undefined) {
      companyID = parseInt(companyID)
      if (!isNaN(companyID)) {
        this.listLoading = true
        const formData = {
          'companyID': companyID
        }
        getCompanyDetail(formData).then(response => {
          if (response.status === 200) {
            this.companyForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(companyID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'company' })
    },
    handleKeywordConfirm:function(){
      let keyword = this.keywordForm.title;
      if (keyword) {
        this.companyForm.keyword.push(keyword);
      }
      this.keywordFormHidden = true;
      this.keywordForm.title = '';
    },
    handleKeywordHidden:function(){
      this.keywordFormHidden = false
      this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleKeywordRemove:function(index){
      this.companyForm.keyword.splice(index, 1);
    },
    loadExchangeList(){
      
      getExchangeAvabile().then(response => {
        this.exchangeOptions = response.data.list
      })
    },
    createData() {
      this.listLoading = true
      const companyForm = this.companyForm
      try {
        companyForm.status = companyForm.status.value
      } catch (error) {
        companyForm.status = 1
        
      }
      console.log(companyForm)
      if (companyForm.ID > 0) {
        companyForm.companyID = companyForm.ID
      }
      saveCompany(companyForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'company' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'companyID': row.ID,
      }
      removeCompanyById(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'company' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
